<?php

namespace app\admin\model;

use think\Model;

class Proxlog extends Model
{
    // 表名
    protected $name = 'proxylog';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'server_type_text',
        'status_text',
        'is_agent_text'
    ];
    

    
    public function getServerTypeList()
    {
        return ['0' => __('Server_type 0'),'1' => __('Server_type 1'),'2' => __('Server_type 2'),'3' => __('Server_type 3')];
    }     

    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1'),'2' => __('Status 2')];
    }     

    public function getIsAgentList()
    {
        return ['1' => __('Is_agent 1'),'0' => __('Is_agent 0')];
    }     


    public function getServerTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['server_type']) ? $data['server_type'] : '');
        $list = $this->getServerTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsAgentTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['is_agent']) ? $data['is_agent'] : '');
        $list = $this->getIsAgentList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function upDeposit($id,$status){
        $data['status']=$status;
        $suc=$this->where(['id'=>$id])->update($data);
        return $suc;
    }

    /**
     * @param $user_id  当前用户id
     * @param $money    开通返佣
     * @return bool     操作返回真假
     */
    public function InsertAagentInverse($user_id,$money){
       try{
           $UserModel=new User();
           $where['id']=array('eq',$user_id);
           $UserModel->where($where)->setInc('money',$money);
       }catch (\Exception $e){
            return false;
       }
       return true;
    }

    public function InsertMoneyLog($data){
      try{
          $MoneyLog=new Usermoney();
          $MoneyLog->isUpdate(false)->data($data, true)->save();
      }catch (\Exception $e){
          return false;
      }
      return true;
    }
}
